package hash;

public class HashBuck2<K,V> {
    static class Node<K,V>{
        public K key;
        public V val;
        public Node<K,V> next;

        public Node(K key, V val) {
            this.key = key;
            this.val = val;
        }
    }

    public Node<K,V>[] table;
    public int size;
    public static final float DEFAULT_LOAD_FACTOR = 0.75f;

    /**
     * 构造方法
     */
    public HashBuck2(){
        table = (Node<K, V>[]) new Node[10];
    }

    public void put(K key,V val){
        int hash = key.hashCode();
        int index = hash % table.length;
        Node<K,V> cur = table[index];
        while(cur != null){
            if (cur.key.equals(key)){//此处不能使用==因为是引用类型
                //更新val
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        Node<K,V> node = new Node(key,val);
        node.next = table[index];
        table[index] = node;
        size++;
        //待补充
    }

    public V getValue(K key){
        int hash = key.hashCode();
        int index = hash % table.length;
        Node<K,V> cur = table[index];
        while (cur != null){
            if (cur.key.equals(key)){
                return cur.val;
            }
            cur = cur.next;
        }
        return null;
    }


}
